<?php
/**
 * WorkOrdersController.php
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-02-18 15:10:40
 * @modified    2023-02-18 15:10:40
 *
 */

namespace App\Http\Controllers;

use App\Repositories\WorkOrderRepo;
use App\Services\WorkOrderService;
use Illuminate\Http\Request;
use function Symfony\Component\Translation\t;

class WorkOrdersController extends BaseModuleController
{
    /**
     * @OA\Get(
     *     path="/wos/params",
     *     tags={"WorkOrder"},
     *     summary="获取工单参数列表",
     *     description="获取工单参数列表",
     *     operationId="wo_params",
     *     security={{"bearerAuth":{}}},
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function params(Request $request)
    {
        return message('获取成功', true, WorkOrderRepo::params());
    }

    /**
     * @OA\Get(
     *     path="/wos",
     *     tags={"WorkOrder"},
     *     summary="获取工单列表",
     *     description="获取工单列表",
     *     operationId="wo_index",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="export", in="query", description="是否导出(需要导出excel时必传: 1)", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="audit_manager", in="query", description="二级审核是否完成(默认或0: 不过滤, 1: 审核完成)", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="audited", in="query", description="当前用户角色是否已审核(默认不传参: 不筛选, 0: 未审核, 1: 已审核)", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="callbacked", in="query", description="当前用户角色是否已回访(默认不传参: 不筛选, 0: 未回访, 1: 已回访)", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="date_type", in="query", description="week, month(本周，本月)", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="date_start", in="query", description="开始时间(带date_type时，该参数无效)", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="date_end", in="query", description="结束时间(带date_type时，该参数无效)", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="about_me", in="query", description="department、assist、master、create（我部门、我协助、我负责、我创建）", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="status", in="query", description="状态(未完成:un_complete、待指派:un_assign、已关闭: closed、已指派:assigned、已接受:accept、进行中:process、已完成:complete, 异常工单: exception)", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="type", in="query", description="工单类型(见类型参数中的: wo_types)", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="date", in="query", description="日期(今天 today, 本周内 week, 本月 month, 本年 year)", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="user_id", in="query", description="负责人ID", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="dept_id", in="query", description="部门ID", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="number", in="query", description="工单编号(模糊匹配)", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="satisfaction", in="query", description="满意度(1,2,3,4,5), 分别为: 非常不满意、不满意、一般、满意、非常满意", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="page", in="query", description="当前页数", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="limit", in="query", description="每页数量(默认10)", required=false, @OA\Schema(default=10, type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function index()
    {
        try {
            $workOrders = WorkOrderRepo::getList();
            $export = intval(request()->get('export') ?? 0);
            if ($export) {
                return message('导出成功', true, $workOrders);
            }
            return list_message('获取成功', true, $workOrders);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Get(
     *     path="/wos/status_counts",
     *     tags={"WorkOrder"},
     *     summary="获取工单各状态数量",
     *     description="获取工单各状态数量",
     *     operationId="wo_status_counts",
     *     security={{"bearerAuth":{}}},
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function status_counts()
    {
        try {
            return message('获取成功', true, WorkOrderRepo::statusCounts());
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Get(
     *     path="/wos/{id}",
     *     tags={"WorkOrder"},
     *     summary="获取工单详情",
     *     description="获取工单详情",
     *     operationId="work_order_show",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id", in="path", description="工单ID", required=true, @OA\Schema(default="", type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function show(Request $request, $id)
    {
        try {
            $workOrder = WorkOrderRepo::fetch($id);
            return message('获取成功', true, $workOrder);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Post(
     *     path="/wos/{id}/reassigned_apply",
     *     operationId="wos_reassigned_apply",
     *     tags={"WorkOrder"},
     *     summary="工单转派申请",
     *     description="工单转派申请",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id", in="path", description="工单ID", required=true, @OA\Schema(default="", type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401, description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function reassigned_apply(Request $request, $id)
    {
        try {
            WorkOrderRepo::reassignedApply($id);
            return message('申请成功', true);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Post(
     *     path="/wos/{id}/reassigned",
     *     operationId="wos_reassigned",
     *     tags={"WorkOrder"},
     *     summary="工单转派",
     *     description="工单转派",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id", in="path", description="工单ID", required=true, @OA\Schema(default="", type="integer")),
     *     @OA\RequestBody(
     *         description="表单数据",
     *         @OA\MediaType(
     *             mediaType="application/x-www-form-urlencoded",
     *             @OA\Schema(
     *                 type="object",
     *                 required={"user_id"},
     *                 @OA\Property(property="user_id", description="负责人ID", type="integer"),
     *             )
     *         )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401, description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function reassigned(Request $request, $id)
    {
        try {
            WorkOrderRepo::reassigned($id);
            return message('转派成功', true);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Post(
     *     path="/wos",
     *     tags={"WorkOrder"},
     *     summary="新建工单",
     *     description="新建工单",
     *     operationId="work_order_store",
     *     security={{"bearerAuth":{}}},
     *     @OA\RequestBody(
     *          required=true,
     *          @OA\JsonContent(
     *              type="object",
     *              required={"customer_id", "service_type", "service_content", "plan_at", "description", "level", "contact_name", "contact_phone", "province_id", "city_id", "county_id", "address"},
     *              @OA\Property(property="event_id", type="integer", example="事件ID", description="事件ID"),
     *              @OA\Property(property="customer_id", type="integer", example="客户ID", description="客户ID"),
     *              @OA\Property(property="type", type="string", example="工单类型（见参数列表）", description="工单类型"),
     *              @OA\Property(property="service_type", type="string", example="服务类型（见参数列表）", description="服务类型"),
     *              @OA\Property(property="service_content", type="string", example="服务内容（见参数列表）", description="服务内容"),
     *              @OA\Property(property="plan_at", type="string", example="计划时间", description="计划时间"),
     *              @OA\Property(property="description", type="string", example="故障现象", description="故障现象"),
     *              @OA\Property(property="level", type="string", example="优先级", description="优先级"),
     *              @OA\Property(property="contact_name", type="string", example="联系人姓名", description="联系人姓名"),
     *              @OA\Property(property="contact_phone", type="string", example="联系人电话", description="联系人姓名"),
     *              @OA\Property(property="province_id", type="integer", example="省份ID", description="省份ID"),
     *              @OA\Property(property="city_id", type="integer", example="城市ID", description="城市ID"),
     *              @OA\Property(property="county_id", type="integer", example="区县ID", description="区县ID"),
     *              @OA\Property(property="address", type="string", example="详细地址", description="详细地址"),
     *              @OA\Property(property="user_id", type="integer", example="负责人ID", description="负责人ID"),
     *              @OA\Property(property="product_ids", type="array",  @OA\Items()),
     *              @OA\Property(property="images", type="array", @OA\Items()),
     *              @OA\Property(property="assist_user_ids", type="array",@OA\Items()),
     *          )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function store(Request $request)
    {
        try {
            $workOrder = WorkOrderRepo::store(0);
            return message('创建成功', true, $workOrder);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Post(
     *     path="/wos/{id}/allot",
     *     tags={"WorkOrder"},
     *     summary="分配工单",
     *     description="分配工单",
     *     operationId="work_order_allot",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id", in="path", description="工单ID", required=true, @OA\Schema(default="", type="integer")),
     *     @OA\RequestBody(
     *          required=true,
     *          @OA\JsonContent(
     *              type="object",
     *              required={"user_id", "assist_user_ids"},
     *              @OA\Property(property="user_id", type="integer", example=0, description="负责人ID"),
     *              @OA\Property(
     *                  property="assist_user_ids",
     *                  type="array",
     *                  @OA\Items(),
     *              )
     *          )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function allot(Request $request, $id)
    {
        try {
            $workOrder = WorkOrderRepo::allot($id);
            return message('分配成功', true, $workOrder);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Post(
     *     path="/wos/{id}/accept",
     *     tags={"WorkOrder"},
     *     summary="工程师接单",
     *     description="工程师接单",
     *     operationId="work_order_accept",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id", in="path", description="工单ID", required=true, @OA\Schema(default="", type="integer")),
     *     @OA\RequestBody(
     *         description="表单数据",
     *         @OA\MediaType(
     *             mediaType="application/x-www-form-urlencoded",
     *             @OA\Schema(
     *                 type="object",
     *                 required={"plat_at"},
     *                 @OA\Property(property="plat_at", description="计划时间", type="string")
     *             )
     *         )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function accept(Request $request, $id)
    {
        try {
            $workOrder = WorkOrderRepo::accept($id);
            return message('接单成功', true, $workOrder);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Post(
     *     path="/wos/{id}/start",
     *     tags={"WorkOrder"},
     *     summary="工程师开始处理工单",
     *     description="工程师开始处理工单",
     *     operationId="work_order_start",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id", in="path", description="工单ID", required=true, @OA\Schema(default="", type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function start(Request $request, $id)
    {
        try {
            $workOrder = WorkOrderRepo::start($id);
            return message('开始成功', true, $workOrder);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Post(
     *     path="/wos/{id}/receipt",
     *     tags={"WorkOrder"},
     *     summary="工单回执",
     *     description="工单回执",
     *     operationId="work_order_receipt",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id", in="path", description="工单ID", required=true, @OA\Schema(default="", type="integer")),
     *     @OA\RequestBody(
     *          required=true,
     *          @OA\JsonContent(
     *              type="object",
     *              required={"reason", "process_type", "description", "signature"},
     *              @OA\Property(property="reason", type="integer", example="故障原因", description="故障原因"),
     *              @OA\Property(property="process_type", type="string", example="处理方式", description="处理方式"),
     *              @OA\Property(property="description", type="string", example="详细内容", description="详细内容"),
     *              @OA\Property(property="signature", type="string", example="签名", description="签名"),
     *              @OA\Property(property="satisfaction", type="string", example="客户满意度", description="客户满意度"),
     *              @OA\Property(property="attitude", type="string", example="服务态度", description="服务态度"),
     *              @OA\Property(property="efficiency", type="string", example="服务效率", description="服务效率"),
     *              @OA\Property(property="images", type="array", @OA\Items()),
     *              @OA\Property(
     *                  property="boms",
     *                  type="array",
     *                  @OA\Items(
     *                      @OA\Property(property="bom_id", type="integer", example="配件ID", description="配件ID"),
     *                      @OA\Property(property="count", type="integer", example="数量", description="数量"),
     *                  )
     *              ),
     *              @OA\Property(
     *                  property="services",
     *                  type="array",
     *                  @OA\Items(
     *                      @OA\Property(property="name", type="string", example="项目名称", description="项目名称"),
     *                      @OA\Property(property="unit", type="string", example="单位", description="单位"),
     *                      @OA\Property(property="count", type="integer", example="数量", description="数量"),
     *                      @OA\Property(property="fee", type="string", example="费用", description="费用")
     *                  )
     *              )
     *          )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function receipt(Request $request, $id)
    {
        try {
            $workOrder = WorkOrderRepo::receipt($id);
            return message('回执成功', true, $workOrder);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Get(
     *     path="/wo_rankings",
     *     tags={"WorkOrder"},
     *     summary="工单处理排行榜",
     *     description="工单处理排行榜",
     *     operationId="wo_ranking",
     *     security={{"bearerAuth":{}}},
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function rankings(Request $request)
    {
        return message('获取成功', true, WorkOrderRepo::rankings());
    }

    /**
     * @OA\Get(
     *     path="/wo_count_statistic",
     *     tags={"WorkOrder"},
     *     summary="工单数量统计",
     *     description="工单数量统计",
     *     operationId="wo_count_statistic",
     *     security={{"bearerAuth":{}}},
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function count_statistic(Request $request)
    {
        return message('获取成功', true, WorkOrderRepo::countStatistic());
    }

    /**
     * @OA\Post(
     *     path="/wos/{id}/audit",
     *     tags={"WorkOrder"},
     *     summary="工单审核",
     *     description="工单审核",
     *     operationId="work_order_audit",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id", in="path", description="工单ID", required=true, @OA\Schema(default="", type="integer")),
     *     @OA\RequestBody(
     *         description="表单数据",
     *         @OA\MediaType(
     *             mediaType="application/x-www-form-urlencoded",
     *             @OA\Schema(
     *                 type="object",
     *                 required={"content"},
     *                 @OA\Property(property="content", description="审核内容", type="string"),
     *             )
     *         )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function audit(Request $request, $id)
    {
        try {
            WorkOrderRepo::audit($id);
            return message('审核成功', true);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Get(
     *     path="/wos/{id}/audit_show",
     *     tags={"WorkOrder"},
     *     summary="查看工单审核结果",
     *     description="查看工单审核结果",
     *     operationId="work_order_audit_show",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id", in="path", description="工单ID", required=true, @OA\Schema(default="", type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function audit_show(Request $request, $id)
    {
        try {
            $result = WorkOrderRepo::audit_show($id);
            return message('获取成功', true, $result);
        } catch (\Exception $e) {
            return message($e->getMessage());
        }
    }

    /**
     * @OA\Get(
     *     path="/wos/{id}/callback_show",
     *     tags={"WorkOrder"},
     *     summary="查看工单回访结果",
     *     description="查看工单回访结果",
     *     operationId="work_order_callback_show",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id", in="path", description="工单ID", required=true, @OA\Schema(default="", type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function callback_show(Request $request, $id)
    {
        try {
            $result = WorkOrderRepo::callback_show($id);
            return message('获取成功', true, $result);
        } catch (\Exception $e) {
            return message($e->getMessage());
        }
    }

    /**
     * @OA\Post(
     *     path="/wos/{id}/callback",
     *     tags={"WorkOrder"},
     *     summary="工单回访",
     *     description="工单回访",
     *     operationId="work_order_callback",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id", in="path", description="工单ID", required=true, @OA\Schema(default="", type="integer")),
     *     @OA\RequestBody(
     *         description="表单数据",
     *         @OA\MediaType(
     *             mediaType="application/x-www-form-urlencoded",
     *             @OA\Schema(
     *                 type="object",
     *                 required={"content"},
     *                 @OA\Property(property="content", description="回访内容", type="string"),
     *                 @OA\Property(property="solved", description="问题解决情况(1: 已解决, 0: 未解决)", type="integer"),
     *                 @OA\Property(property="content_check", description="内容核对情况(fit: 一致, deviation: 有偏差)", type="string"),
     *                 @OA\Property(property="satisfaction", description="满意度(low: 一般1分, mid: 满意5分, high: 很满意10分)", type="string"),
     *             )
     *         )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function callback(Request $request, $id)
    {
        try {
            WorkOrderRepo::callback($id);
            return message('回访成功', true);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Post(
     *     path="/wos/{id}/close",
     *     tags={"WorkOrder"},
     *     summary="关闭工单",
     *     description="关闭工单",
     *     operationId="work_order_close",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id", in="path", description="工单ID", required=true, @OA\Schema(default="", type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function close(Request $request, $id)
    {
        try {
            WorkOrderRepo::closeWorkOrder($id);
            return message('关闭成功', true);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }
}